<template>
  <div class="test">
    <div class="echarts" ref="echartRef" />
    <button @click="btnClick">点击</button>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, nextTick } from "vue";
import useEcharts from "./useEcharts";
import { EChartsOption } from "echarts";
export default defineComponent({
  name: "test",
  setup() {
    const echartRef = ref<HTMLElement>();
    const echartResizeFunc = ref<() => void>();
    onMounted(() => {
      const { echartInstance, echartSetOption, echartResize } = useEcharts(
        echartRef.value as HTMLElement
      );
      echartResizeFunc.value = echartResize;
      const obj: EChartsOption = {
        title: {
          text: "ECharts 入门示例",
        },
        tooltip: {},
        xAxis: {
          data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      };
      echartSetOption(obj);
    });
    const btnClick = async () => {
      (echartRef.value as HTMLElement).style.width = "400px";
      await nextTick();
      (echartResizeFunc.value as () => void)();
    };
    return {
      echartRef,
      btnClick,
    };
  },
});
</script>
<style scoped>
.test {
  width: 100%;
  height: 100%;
  background-color: #eee;
}
.echarts {
  width: 60%;
  height: 50%;
  background-color: #ccc;
}
</style>
